package com.test;

import java.io.FileNotFoundException;
import java.net.ConnectException;
import java.net.NoRouteToHostException;
import java.net.SocketTimeoutException;

import com.acrm.client.exception.CannotConnectException;
import com.acrm.client.exception.InteractionException;
import com.acrm.client.exception.ServerThrowException;
import com.pccw.hessian.support.client.ExceptionHandler;

public class CRMExceptionHandler implements ExceptionHandler{

	@Override
	public Throwable parseException(Throwable t) {
		//t.printStackTrace();
		t=CRMUtils.getCaused(t);
		if(t instanceof ServerThrowException){
			return (InteractionException) t;
		}if(t instanceof ConnectException){
			return new CannotConnectException("Connect refused", 2);//连接被拒绝
		}else if(t instanceof SocketTimeoutException){//connect timed out 21		Read timed out 14
			return new CannotConnectException("Connect timed out",t, 1);//连接超时//网络不畅通
		}else if(t instanceof NoRouteToHostException){
			return new CannotConnectException("No routeto host", 0);//没有到主机的路由
		}else if(t instanceof SocketTimeoutException){
			return new CannotConnectException("Read timed out", 1);//读取数据超时
		}else if(t instanceof FileNotFoundException){
			return new ServerThrowException("Cannot connect to Server",t, 0);//连接失败，导致此异常极有可能服务端异常405
		}else{
			return new CannotConnectException("连接失败", t,0);
		}
	}

}
